<?php
class Controller_App_Register extends Controller_App_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:app/register";
        parent::__construct($req, $res);
    }

    public function action_index ($register_status = null)
    {
        if (! empty($this->_post))
        {
            try {
	            $mobile = Arr::get($this->_post, 'mobile');
	            $code   = Arr::get($this->_post, 'code');
	            $password = Arr::get($this->_post, 'password');
	
	            $uid = $this->_user->register($mobile, $code, $password);
	            $this->request->redirect('/');
            }
            catch (Exception $e)
            {
                //
                $this->request->redirect('/register/index/1');
            }
                
        }
        else
        {
            if($register_status == 1){
                $this->template->set('register_status', 1);
            }else{
                $this->template->set('register_status', 0);
            }
        }
        
        $title_json  = App_Util::buildClientTitleJson('back', 'block', '注册', 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson('none', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }
    
    public function action_mobileSendCode2 ()
    {
        $mobile = Arr::get($this->_post, 'mobile');
        
        $new_user = new User_V2_Guest(); 
        $new_user->sendMobileCode($mobile); 
        $ret    = array('status' => 0, 'mobile' => $mobile);

        $this->json_encode($ret);
    }
  
    public function action_mobileSendCode ()
    {
        $mobile = Arr::get($this->_post, 'mobile');
        
        $isRegister = App_Register::hasRegishter($mobile);
        if($isRegister){
            $ret    = array('status' => 1, 'mobile' => $mobile);
            $this->json_encode($ret);
            exit;
        }

        $this->_user->sendMobileCode($mobile); 
        $ret    = array('status' => 0, 'mobile' => $mobile);

        $this->json_encode($ret);
    }

    public function before ()
    {
        parent::before();
        if ($this->_uid > 0)
        {
            $this->request->redirect('/');
            exit;
        }
    }
}
